<template>
  <div class="login">
    <el-input v-model="username" placeholder="请输入账号"></el-input>
    <el-input v-model="password" placeholder="请输入密码" type="password"></el-input>
    <el-button @click="login">登录</el-button>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        username:'',
        password:''
      }
    },
    methods: {
      login(){
        this.$http.post('/user/signin',{
          username:this.username,
          password:this.password
        }).then(res => {
          if(res.errcode == 0){
            this.$store.commit('setUser', res.data)
            localStorage.setItem('token',res.data.token)

            this.$http.get('/user/info').then(res => {
              if (res.errcode == 0){
                this.$store.commit('setUser', res.data)
              }else{
                router.push('/login')
              }
            })  

            this.$router.push('/')
          }else{
            this.$message.error(res.errmsg)
          }
        })
      }
    },
  }
</script>
